#include "Weatherpage.h"
#include "arduino.h"
#include "lvgl.h"
#include "../StartPage/WiFi_AT.h"
#include "../Resource/ResourcePool.h"



/*************************************************
 *  函数名称 :  Create
 *  参    数 ： 无
 *  函数功能 ： 界面创建函数
 *************************************************/
void WeatherView::Create(lv_obj_t *root)
{
    lv_obj_remove_style_all(root);
    lv_obj_set_size(root, LV_HOR_RES, LV_VER_RES);

    lv_obj_set_scrollbar_mode(root, LV_SCROLLBAR_MODE_OFF);  // 关闭滑动条
    
    // 城市信息创建
    lv_obj_t* city_label = lv_label_create(root);
    lv_obj_set_style_text_font(city_label, Resource.GetFont("city_cn_10"), 0);
    lv_obj_set_style_text_color(city_label, lv_color_hex(0x000000), 0);
    lv_label_set_text(city_label, "石家庄");
    lv_obj_set_pos(city_label, 15, 25);
    ui.city_label = city_label;

    // 城市位置创建
    lv_obj_t *distantion = lv_img_create(root);
    lv_img_set_src(distantion, Resource.GetImage("distantion"));
    //lv_obj_align(weather, LV_ALIGN_CENTER, -50, -26);  // 居中对齐 设定偏移量
    lv_obj_set_pos(distantion, 120, 20);

    // 创建风格
    static lv_style_t style_shadow;
    lv_style_init(&style_shadow);
    lv_style_set_bg_color( &style_shadow, lv_color_hex(0x97bff2) );
    lv_style_set_bg_grad_color( &style_shadow, lv_color_hex(0x6998f2) );
    lv_style_set_bg_grad_dir( &style_shadow, LV_GRAD_DIR_VER );  // 创建过渡颜色

    // 创建天气背板
    lv_obj_t *weatherback = lv_obj_create(root);
    lv_obj_set_size(weatherback, 220, 120);
    //lv_obj_align(weatherback, LV_ALIGN_CENTER, 0, 20);  // 居中对齐 设定偏移量
    lv_obj_set_pos(weatherback, 10, 115);
    lv_obj_add_style(weatherback, &style_shadow, LV_PART_MAIN);

    // 天气图片创建
    lv_obj_t *weather = lv_img_create(root);
    lv_img_set_src(weather, Resource.GetImage("Cloud_angled_rain_zap"));
    //lv_obj_align(weather, LV_ALIGN_CENTER, -50, -26);  // 居中对齐 设定偏移量
    lv_obj_set_pos(weather, 23, 95);
    ui.weather = weather;

    // 天气温度创建
    lv_obj_t* label_tem = lv_label_create(root);
    lv_obj_set_style_text_font(label_tem, Resource.GetFont("num_56"), 0);
    lv_obj_set_style_text_color(label_tem, lv_color_hex(0xBAD8FE), 0);
    lv_label_set_text(label_tem, "27");
    lv_obj_set_pos(label_tem, 115, 115);
    ui.label_tem = label_tem;

    lv_obj_t* label = lv_label_create(root);
    lv_obj_set_style_text_font(label, Resource.GetFont("tem"), 0);
    lv_obj_set_style_text_color(label, lv_color_hex(0xBAD8FE), 0);
    lv_label_set_text(label, "℃");
    lv_obj_set_pos(label, 190, 160);
    
    // 天气状态
    lv_obj_t* text_label = lv_label_create(root);
    lv_obj_set_style_text_font(text_label, Resource.GetFont("en_18"), 0);
    lv_obj_set_style_text_color(text_label, lv_color_hex(0xf3f7ff), 0);
    lv_label_set_text(text_label, "Rain");
    lv_obj_set_pos(text_label, 35, 180);
    ui.text_label = text_label;

    // 更新时间
    lv_obj_t* updata_label = lv_label_create(root);
    lv_obj_set_style_text_font(updata_label, Resource.GetFont("time_10"), 0);
    lv_obj_set_style_text_color(updata_label, lv_color_hex(0xf3f7ff), 0);
    lv_label_set_text(updata_label, "2022-08-31T21:07");
    lv_obj_set_pos(updata_label, 35, 210);
    ui.updata_label = updata_label;
}




// const uint8_t Sunny[] = {0, 1, 2, 3};
// const uint8_t Cloud[] = {4, 5, 6, 7, 8, 9};
// const uint8_t Rain[] = {10, 11, 12, 13, 14, 15, 16, 17, 18};
// const uint8_t Snow[] = {19, 20, 21, 22, 23, 24, 25};
// const uint8_t Winds[] = {26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36};

#define Sunny 0
#define Cloud 1
#define Rain 2
#define Snow 3
#define Winds 4


uint8_t WeatherPic(char *code)
{
    uint8_t weathercode = atoi(code);

    switch (weathercode)
    {
    case 0 ... 3:
        return Sunny;
        break;
    
    case 4 ... 9:
        return Cloud;
        break;

    case 10 ... 18:
        return Rain;
        break;

    case 19 ... 25:
        return Snow;
        break;

    case 26 ... 36:
        return Winds;
        break;
    default:
        break;
    }
}

WeatherView ViewWeather;

void WeatherInit(void)
{
    lv_obj_t* root = lv_label_create(lv_scr_act());
    ViewWeather.Create(root);
}

void WeatherShow(char *City, Weather_Info_t *info)
{
    switch(WeatherPic(info->code))
    {
        case Sunny: lv_img_set_src(ViewWeather.ui.weather, Resource.GetImage("Sun"));break;
        case Cloud: lv_img_set_src(ViewWeather.ui.weather, Resource.GetImage("Cloud"));break;
        case Rain: lv_img_set_src(ViewWeather.ui.weather, Resource.GetImage("Cloud_angled_rain_zap"));break;
        case Snow: lv_img_set_src(ViewWeather.ui.weather, Resource.GetImage("Big_snow"));break;
        case Winds: lv_img_set_src(ViewWeather.ui.weather, Resource.GetImage("Slow_winds"));break;
    }

    lv_label_set_text(ViewWeather.ui.city_label, City);
    // 天气状态判断
    if(strcmp("Clear", info->text))
        lv_label_set_text(ViewWeather.ui.text_label, info->text);
    else
        lv_label_set_text(ViewWeather.ui.text_label, "Sun");
    lv_label_set_text(ViewWeather.ui.label_tem, info->temp);
    char *updatatime;  // 16
    strncpy(updatatime, info->update, 16);
    lv_label_set_text(ViewWeather.ui.updata_label, updatatime);
}